<!DOCTYPE html>
<html lang="en" xmlns:range="http://www.w3.org/1999/xhtml">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <script src="/js/jquery-3.7.1.min.js"></script>
    <link rel="stylesheet" href="/plugins/bootstriptable/bootstrap-table.min.css">
    <script src="/plugins/bootstriptable/bootstrap-table.min.js"></script>
    <script src="/plugins/bootstriptable/locale/bootstrap-table-zh-CN.min.js"></script>
    <style>

        #teas{
            background-color: #2aabd2;
            height: 70px;
            font-size: 3em;
        }
        #t1{
            float: left;
            margin-left: 100px;
        }
        #t2{
            color: #5e5e5e;
            float: left;
        }
        #t3{
            float:right;
            margin-right: 30px;
        }
        #footer{
            margin-top: 30px;
            height:50px;
            text-align: center;
            color: #000;
            font-size: 1em;
        }
        #f3{
            font-size: 1.8em;
            color: #154a9f;
        }
        #st{
            height: 400px;
        }
        #stuTable{
            background-color: #faf2cc;
        }
    </style>
</head>
<body>
<div id="teas">
    <div id="t1">学生管理系统</div>
    <div id="t2">（学校端）</div>
    <div id="t3">
        <button type="submit" class="btn btn-success" onclick="backLogin()">退出登陆</button>
    </div>
</div>
<div class="container-fluid" id="st">
    <div class="title" >

        <form class="form-inline">
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" onclick="preAdd();">
                新增学生
            </button>
            <div class="form-group">
                <label for="searchName">请输入名字</label>
                <input type="text" class="form-control" id="searchName" placeholder="请输入名称" name="searchName">
            </div>
            <button type="button" class="btn btn-default btn-primary" onclick="search();">查询</button>
        </form>

    </div>
    <div class="table-responsive">
        <table class="table table-bordered" id="stuTable">

        </table>
    </div>
</div>

<div id="footer">
    <div id="f1">
        <a href="https://www.ctbu.edu.cn/index.htm">学校官网</a>
        |
        <a href="http://eol.ctbu.edu.cn/meol/homepage/common/">在线学习平台</a>
        |
        <a href="http://www.cnki.net/">中国知网</a>
        | 重庆工商大学版权所有©1997-2023
    </div>

    <div id="f2">2021级 计算机四班 刘成</div>
    <div id="f3">重庆工商大学</div>
</div>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">
                <form id="formStudent">
                    <div class="form-group">
                        <input hidden="hidden" id="id" name="id">
                    </div>
                    <div class="form-group">
                        <label for="no">学号</label>
                        <input type="text" class="form-control" id="no" name="no" placeholder="编号" required
                        minlength="10" maxlength="10">
                    </div>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="姓名"required
                        minlength="2" maxlength="20">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别:</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="1">男</option>
                            <option value="2">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="年龄" required
                        min="16" max="25">
                    </div>
                </form>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success" onclick="saveStudent()">保存</button>
                </div>
                <!-表单结束->
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" tabindex="-1" role="dialog" id="myModal2">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选课信息</h4>
            </div>
            <div class="modal-body">
                <form id="formStudent2">
                    <div class="form-group">
                        <input hidden="hidden" id="id2" name="id2" disabled>
                    </div>
                    <div class="form-group">
                        <label for="no">学号</label>
                        <input type="text" class="form-control" id="no2" name="no2" placeholder="编号" required
                               minlength="10" maxlength="10" disabled>
                    </div>
                    <div class="form-group">
                        <label for="course2">课程:</label>
                        <select class="form-control" id="course2" name="course2" placeholder="课程">
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="grade2">成绩</label>
                        <input type="text" class="form-control" id="grade2" name="grade2" placeholder="成绩" required
                               range:[0,100]>
                    </div>
                </form>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-success" onclick="saveCourse()">保存</button>
                </div>
                <!-表单结束->
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" tabindex="-1" role="dialog" id="myModal3">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">课程信息</h4>
            </div>

            <div class="modal-body">
                <div class="table-responsive">
                    <table class="table table-bordered" id="tcTable">
                    </table>
                </div>

                <div class="modal-footer">
                    <button type="submit" class="btn btn-success" onclick="createCourse()">新增</button>
                </div>
                <!-表单结束->
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script src="/plugins/bootstriptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstriptable/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/plugins/jqvalidate/jquery.validate.min.js"></script>
<script src="/plugins/jqvalidate/localization/messages_zh.min.js"></script>


<script type="text/javascript">
    var stsno;
    function backLogin(){
        window.location.href="http://localhost:8080/login/user";
    }
    //保存选课
    function saveCourse(){
        var ttno = location.search;
        ttno = ttno.slice(ttno.indexOf("=") + 1);
        var pass = $("#formStudent").valid()
        if (pass) {
            var data = {
                id: 0,
                sno: stsno,
                tno: $("#course2").val(),
                grade: $("#grade2").val(),
            }
            var ssno = stsno;
            var data2 = {
                tno: $("#course2").val(),
            }
            $.ajax({
                url: "/webapi/score/gettno",
                method: "get",
                data: data2,
            }).done(function (rs) {
                var x = 0;
                for (var i in rs.data) {
                    if (ssno == rs.data[i].sno) {
                        x = -1;
                        alert("这名学生已经选择了这门课程！");
                    }
                }
                if (x != -1) {
                    $.ajax({
                        url: "/webapi/score/insert",
                        method: "post",
                        data: data
                    }).done(function () {
                        loadTeacher();
                        loadScore();
                        $('#myModal2').modal('hide')
                    })
                }
            })
        }
    }

    //添加选课
    function createCourse(){
        $("#myModal2").modal("show");
        $("#myModal3").modal("hide");
        $.ajax({
            url: "/webapi/teacher/getall",
            method: "get",
        }).done(function(rs){
            $("#no2").val(stsno);
            for(var i in rs.data){
                var select = document.getElementById("course2");
                var option = document.createElement("option");
                option.text = rs.data[i].course;
                option.value = rs.data[i].no;
                select.add(option);
            }
        })
    }
    //新增或更新
    function saveStudent(){
        var pass = $("#formStudent").valid()
            if (pass)
            {
                var data=$("#formStudent").serialize();
                var id=$('#id').val();
                if (id <1){//是新增
                    $.ajax({
                        url:"/webapi/student/insert",
                        method:"post",
                        data:data
                    }).done(function(){
                        loadTable();
                        $('#myModal').modal('hide')
                    })
                }
                else{//是更新
                    $.ajax({
                        url:"/webapi/student/update",
                        method:"put",
                        data:data
                    }).done(function(){
                        loadTable();
                        $('#myModal').modal('hide')
                    })
                }
            }
    }
    /*
    function deleteStudent(){
        var dedata={
            id:$("#id").val(),
            no:$("#no").val(),
            name:$("#name").val(),
            sex:$("#sex").val(),
            age:$("#age").val(),
            score:$("#score").val(),
        };
        $.ajax({
            url:"/webapi/student/delete",
            method:"delete",
            data: dedata
        }).done(function(){
            loadTable();
            $('#myModal').modal('hide')
        })
    }
    */
    ////
    function deleteStudent(id){
        if(confirm("你是否真的要删除吗？别后悔哦！")){
            var text;

            $.ajax({
                url:'/webapi/student/get/'+id,
                method: 'get',
                async: false,
            }).done(function(rs){
                text = rs.data.no;
            })
            $.ajax({
                url:"/webapi/score/getsno/"+text,
                method: 'get',
                async: false,
            }).done(function(rs){
                for (var i = 0;i < rs.data.length;i++){
                    $.ajax({
                        url: "/webapi/score/delete/"+rs.data[i].id,
                        method: "delete",
                    }).done(function(){
                    })
                }
            })
            $.ajax({
                url:"/webapi/student/delete/"+id,
                method:"delete"
            }).done(function (){
                loadTable();
            })

        }
    }
    /*
    function updateStudent(){
        var updata={
            id:$("#id").val(),
            no:$("#no").val(),
            name:$("#name").val(),
            sex:$("#sex").val(),
            age:$("#age").val(),
            score:$("#score").val(),
        };
        $.ajax({
            url:"/webapi/student/update",
            method:"put",

            data: updata
        }).done(function(){

            loadTable();
            $('#myModal').modal('hide')

        })
    }
    */
    function editStudent(id){
        $('#myModal').modal('show');
        $.ajax({
            url:'/webapi/student/get/'+id
        }).done(function(rs){
            $("#id").val(rs.data.id);
            $("#no").val(rs.data.no);
            $("#name").val(rs.data.name);
            $("#password").val("");
            $("#sex").val(rs.data.sex);
            $("#age").val(rs.data.age);
        })
    }
    function editTC(no){
        $("#myModal3").modal("show");
        $("#tcTable").bootstrapTable("destroy")
        $("#tcTable").bootstrapTable({
            url: "/webapi/score/getbypage",
            striped: true,//设置为true会有隔行变色效果
            pagination: true,//设置为true会在底部显示部分页条
            singleSelect: false,//设置为true将禁止多选
            pageSize: 9,//如果设置了分页，每页数据条数
            pageNumber: 1,//如果设置了分页，煤业页码
            sidePagination: "server",//设置在哪里进行分页，可选值为"client"或
            responseHandler:function (rs){
                if(rs.code > 0){
                    return rs.data;
                }else{
                    console.error("加载数据出错了！",rs.msg);
                    return [];
                }
            },
            queryParams: function (params) {
                var paraObj = {
                    size: params.limit,
                    page: params.offset/params.limit,
                    sort: "id",
                    direct: "ASC",
                    sno: no,
                };
                return paraObj;
            },
            columns: [{
                field: 'id',
                align: 'center',
                valign: 'middle',
                title: '序号',
            },{
                field: 'tno',
                align: 'center',
                valign: 'middle',
                title: '教师编号',
            },{
                field: 'tno',
                align: 'center',
                valign: 'middle',
                title: '姓名',
                formatter: function(value){
                    var a = "";
                    $.ajax({
                        url: '/webapi/teacher/gett/'+value,
                        async: false,
                    }).done(function(row){
                        a = a + row.data.name;
                    })
                    return a;
                }
            },{
                field: 'tno',
                align: 'center',
                valign: 'middle',
                title: '课程',
                formatter: function(value){
                    var a = "";
                    $.ajax({
                        url: '/webapi/teacher/gett/'+value,
                        async: false,
                    }).done(function(rs){
                        a = a + rs.data.course;
                    })
                    return a;
                }
            },{
                field: 'grade',
                align: 'center',
                valign: 'middle',
                title: '成绩',
            },{
                field: 'id',
                align: 'center',
                valign: 'middle',
                title: '操作',
                formatter: function(value){
                    return "<a href='#' onclick='deleteCourse("+value+");'>删除</a>";
                }
            }],
        })
    }
    //删除选课
    function deleteCourse(id){
        if(confirm("你是否真的要删除吗？别后悔哦！")) {
            $.ajax({
                url: "/webapi/score/delete/" + id,
                method: "delete",
            }).done(function() {
                loadTable();
                $("#myModal3").modal("hide");
            })
        }
    }
    $("#stuTable").on("dbl-click-row.bs.table",function(e,row,$element){
        stsno = row.no;
        editTC(row.no);
    })
    /*
    function setButton(){
        $.ajax({

        }).done(function(rs){
            var html="<button type='button' class='btn btn-success' onclick='addStudent();'>保存</button>"
            $("#buttonstudent").html(html);
        })
    }*/
    function loadTable(){
        $('#stuTable').bootstrapTable("destroy")
        $('#stuTable').bootstrapTable({
            url: "/webapi/student/getbypage",
            striped: true,//设置为true会有隔行变色效果
            pagination: true,//设置为true会在底部显示部分页条
            singleSelect: false,//设置为true将禁止多选
            pageSize: 7,//如果设置了分页，每页数据条数
            pageNumber: 1,//如果设置了分页，煤业页码
            sidePagination: "server",//设置在哪里进行分页，可选值为"client"或
            responseHandler:function (rs){
                if(rs.code > 0){
                    return rs.data;
                }else{
                    console.error("加载数据出错了！",rs.msg);
                    return [];
                }
            },
            queryParams: function (params) {
                var paraObj = {
                    size: params.limit,
                    page: params.offset/params.limit,
                    sort: "id",
                    direct: "ASC",
                    name: $("#searchName").val(),
                };
                return paraObj;
            },
            columns: [{
                field: 'id',
                align: 'center',
                valign: 'middle',
                title: '序号'
            }, {
                field: 'no',
                align: 'center',
                valign: 'middle',
                title: '学号'
            },{
                field: 'name',
                valign: 'middle',
                align: 'center',
                title: '姓名'
            }, {
                field: 'sex',
                align: 'center',
                valign: 'middle',
                title: '性别'
            },  {
                field: 'age',
                align: 'center',
                valign: 'middle',
                title: '年龄'
            }, {
                field: 'id',
                title: '操作',
                align: 'center',
                valign: 'middle',
                formatter: function(value){
                    return "<a href='#' onclick='editStudent("+value+");'>编辑</a>    <a href='#' onclick='deleteStudent("+value+");'>删除</a>";
                }
            } ],
        })
    }//end of loadTable();
    $(function(){
        loadTable();
    })
    function search(){
        $('#stuTable').bootstrapTable('destroy');
        loadTable();
    }
    function preAdd(){
        $('#id').val(0);//提示新增
    }



</script>
</body>
</html>